import traceback
import logging
from flask import g, request
from common.utils.jwt_token import verify_token


def jwt_authenrization():
    """
    解析token
    """
    g.user_id = None
    g.is_refresh = False

    token = request.headers.get('Authorization')
    if token:
        token = token[7:]
        try:
            payload = verify_token(token)
            if payload:
                g.user_id = payload.get('user_id')
                g.is_refresh = payload.get('is_refresh', False)
        except:
            error = traceback.format_exc()
            logging.error('jwt_authenrization is error {}'.format(error))
